<?php

namespace App\Http\Controllers\Admin;

use App\Models\Apartment;
use App\Services\Admin\AlbumService;
use App\Services\Admin\ApartmentService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ApartmentController extends Controller
{
    protected $service;
    protected $apartment;

    public function __construct(AlbumService $service, ApartmentService $apartment)
    {
        $this->service = $service;
        $this->apartment = $apartment;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $property_id = $request->get('property_id')??0;
        $property = '';
        if($property_id){
            $property = $this->service->getAlbumPropertyById($property_id);
        }
        return view('admin.apartment.index',compact('property_id','property'));
    }

    public function data(Request $request)
    {

        $model = Apartment::query();
        if ($request->get('title')){
            $model = $model->where('title','like','%'.$request->get('title').'%');
        }
        if ($request->get('property_id')){
            $model = $model->where('property_id',$request->get('property_id'));
        }

        $res = $model->orderBy('property_id','desc')->with(['attribute','property'])->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //分类
        $categorys = $this->apartment->getApartmentCategory();
        $property = $this->service->getAlbumPropertyById($request->get('property_id'));
        return view('admin.apartment.create',compact('categorys','property'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        // 序列化多图
        if(isset($data['images']) && count($data['images'])){
            $data['images'] = serialize($data['images']);
        }
        $property_id = $request->input('property_id')??0;
        Apartment::create($data);
        return redirect("/admin/apartment?property_id=$property_id")->with(['status'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $apartment = Apartment::with(['category','property'])->findOrFail($id);
        if (!$apartment){
            return redirect(route('admin.apartment'))->withErrors(['status'=>'户型不存在']);
        }
        //分类
        $categorys = $this->apartment->getApartmentCategory();
        $property = $this->service->getAlbumPropertyById($apartment->property_id);

        return view('admin.apartment.edit',compact('apartment','categorys','property'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $apartment = Apartment::with(['category','property'])->findOrFail($id);
        $data = $request->all();
        // 序列化多图
        if(isset($data['images']) && count($data['images'])){
            $data['images'] = serialize($data['images']);
        }
        if ($apartment->update($data)){
            return redirect("/admin/apartment?property_id=$apartment->property_id")->with(['status'=>'更新成功']);
        }
        return redirect("/admin/apartment?property_id=$apartment->property_id")->withErrors(['status'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        foreach (Apartment::whereIn('id',$ids)->get() as $model){
            //删除图片
            $model->delete();
        }
        return response()->json(['code'=>0,'msg'=>'删除成功']);
    }

    public function searchProperty(Request $request)
    {
        return $this->service->getSearchProperty($request->input('name'));
    }
}
